package org.cogaen.java2d.xml;

import java.awt.Font;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.cogaen.core.Core;
import org.cogaen.logging.LoggingService;
import org.cogaen.resource.ResourceManager;

public class FontXmlHandle extends XmlHandle {
	
	private static final String LOGGING_SOURCE = ResourceManager.LOGGING_SOURCE;
	private FontParser fontParser;
	
	public FontXmlHandle(String resourceName, String filename) {
		super(resourceName, filename, FontParser.createFilter(resourceName));
		this.fontParser = new FontParser();
	}

	@Override
	protected Object parseXml(Core core, XMLStreamReader parser) throws XMLStreamException {		
		try {
			this.fontParser.parse(parser);
			return new Font(this.fontParser.getName(), this.fontParser.getStyle(), this.fontParser.getSize());			
		} catch (ParseException e) {
			LoggingService logger = LoggingService.getInstance(core);
			logger.logWarning(LOGGING_SOURCE, "unable to load font '" + getName() + "': " + e.getMessage());
			return new Font(FontParser.DEFAULT_NAME, FontParser.DEFAULT_STYLE, FontParser.DEFAULT_SIZE);		
		}
	}

}